package com.winit.commons.util;

import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.Hashtable;

public class SeqGenerate {
    private static Hashtable<String, Object> idHashtable;
    private final static String defult = "DEF";

    static {
        idHashtable = new Hashtable<String, Object>();
        //默认
        SeqCreateVo deVo = new SeqCreateVo(0, DateUtil.formatDate(new Date(), "yyyyMMdd"), defult);

        idHashtable.put(defult, deVo);
    }

    /**
     * 注册一个开头
     *
     * @param index
     */
    public synchronized static void registerIndex(String index) {
        SeqCreateVo deVo = new SeqCreateVo(0, DateUtil.formatDate(new Date(), "yyyyMMdd"), index);
        idHashtable.put(index, deVo);
    }

    /**
     * 生成序号
     *
     * @param title   开头字母
     * @param numbers 序号位数
     * @return
     */
    public synchronized static String getSeqId(String title, String numbers) {
        if (StringUtils.isEmpty(title)) {
            title = defult;//开头字母为空时，默认使用
        }

        if (!idHashtable.containsKey(title)) {
            registerIndex(title);
        }

        String id = "";
        SeqCreateVo seqVo = (SeqCreateVo) idHashtable.get(title);
        String curDate = DateUtil.formatDate(new Date(), "yyyyMMdd");
        if (curDate.equals(seqVo.getCurDate())) {
            long num = seqVo.getNum() + 1;
            if (String.valueOf(num).length() > Integer.valueOf(numbers)) {
                num = 1;
            }
            seqVo.setNum(num);
            id = String.format("%0" + numbers + "d", num);
        } else {
            seqVo.setNum(1);
            seqVo.setCurDate(curDate);
            id = String.format("%0" + numbers + "d", seqVo.getNum());
        }
        idHashtable.put(title, seqVo);
        return id;
    }


    public synchronized static String createId(String index) {
        return createId("yyyyMMddHHmmss", index, "5");
    }

    /**
     * 创建id
     *
     * @param dateStr 时间格式
     * @param index   要生成id的开头字符串
     * @param numbers 最后面的顺序数字是几位的
     * @return
     */
    public synchronized static String createId(String dateStr, String index, String numbers) {
        String strCurDate = DateUtil.getCurDateStr(dateStr);
        String rd = SeqGenerate.getSeqId(index, numbers);
        StringBuffer id = new StringBuffer();
        id.append(index.toUpperCase()).append(strCurDate).append(rd);
        return id.toString();
    }

    /**
     * 创建全局唯一的序列号 36进制
     * @return
     */
    public synchronized static String createSerialNo() {
        String strCurDate = DateUtil.getCurDateStr("yyyyMMddHHmmssSSS");
        String rd = SeqGenerate.getSeqId("SerialNo", "2");
        StringBuffer id = new StringBuffer();
        id.append(strCurDate).append(rd);
        return Long.toString(Long.parseLong(id.toString()), 36).toUpperCase();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            System.out.println(createId( "aaa"));
        }
    }

}
